package com.woniu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.MaintenancePerson;
import com.woniu.service.MaintenancePersonService;
import com.woniu.util.PageData;
import com.woniu.util.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.management.ManagementPermission;
import java.util.List;

@RestController
@RequestMapping("/person")
@Slf4j
@CrossOrigin(origins = "*")
public class MaintenancePersonController {

    @Autowired
    private MaintenancePersonService maintenancePersonService;

        /*
        * 新增
        * */
    @PostMapping("/addPerson")
    public ResponseData addPerson(@RequestBody MaintenancePerson maintenancePerson) {
        log.info("运维人员数据-----> {}", maintenancePerson);
        maintenancePersonService.savePerson(maintenancePerson);
        return ResponseData.ok("用户添加成功");
    }

    /*
    * 删除
    * */
    @DeleteMapping("/deletePerson/{userNumber}")
    public ResponseData deleteUser(@PathVariable("userNumber") List<String> userNumber) {
        maintenancePersonService.removePersonById(userNumber);
        return ResponseData.ok("用户删除成功");
    }

    /*
    * 修改
    * */
    @PostMapping("/updatePerson")
    public ResponseData updateUser(@RequestBody MaintenancePerson maintenancePerson) {
        maintenancePersonService.updatePersonById(maintenancePerson);
        return ResponseData.ok("用户信息更新成功");
    }

    /*
    * 个人查询
    * */
    @GetMapping("/getById")
    public ResponseData getById(@RequestParam("userNumber") String userNumber){
        log.info("id------------> {}", userNumber);
        MaintenancePerson maintenancePerson = maintenancePersonService.getbyPersonId(userNumber);

        return ResponseData.ok(maintenancePerson);
    }

    /*
    * 分页查询
    * */
    @RequestMapping("/pagePerson")
    public ResponseData pagePerson(@RequestBody PageData pageData){
        Page page = maintenancePersonService.queryPage(pageData);

        return ResponseData.ok(page);
    }

    /*
    * 状态
    * */
    @PostMapping("updateStatus/{status}/{userNumber}")
    public ResponseData updateStatus(@PathVariable Integer status, @PathVariable String userNumber){
        maintenancePersonService.updateStatus(status, userNumber);

        return ResponseData.ok();
    }


    @RequestMapping("/getUserInfo")
    public ResponseData getUserInfo(String userName){
        List<MaintenancePerson>maintenancePersons=maintenancePersonService.getUserInfo(userName);

        return ResponseData.ok(maintenancePersons);
    }


    @GetMapping("/personExcel")
    public ResponseData<?> outDepartmentExcelTemplate(HttpServletResponse response) throws IOException {
        return maintenancePersonService.outDepartmentExcelTemplate(response);
    }
}
